package server;

import interfaces.*;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.*;

import javax.swing.JTextArea;

/**
 * ServerReceiver
 * receiver thread
 * Thread for reading socket information
 * 
 * pass input message to socketHandler--the mediator colleague
 * @author Yaxing Chen
 *
 */
class ServerReceiver extends Thread{
	
    private DataInputStream dis;
    
    private JTextArea area;
    
    private int id; //id #, corresponding to a certain socket
    
    private SocketHandler socketHandler;//hold corresponding socketHandler
    
    public ServerReceiver(DataInputStream d,JTextArea a, int idNum, SocketHandler socketHandler){
        this.dis = d;
        this.area = a;
        this.id = idNum;
        this.socketHandler = socketHandler;
    }
    
    public void run(){
        String input;
        while(true){
            try{
            	if(dis == null){
            		break;
            	}
            	input = dis.readUTF();
            	
            	if(!socketHandler.accept(input)){
            		break;
            	}
            }catch(IOException e){
                //e.printStackTrace();
                break;
            }
        }
    }
}